package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.PersonRelationship;
import com.example.demo.service.PersonRelationshipService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/personRelationship")
public class PersonRelationshipController {

    @Resource
    private PersonRelationshipService personRelationshipService;

    // 新增关系
    @PostMapping("/add")
    public Result add(@RequestBody PersonRelationship relationship) {
        personRelationshipService.add(relationship);
        return Result.success();
    }

    // 更新关系
    @PutMapping("/update")
    public Result update(@RequestBody PersonRelationship relationship) {
        personRelationshipService.update(relationship);
        return Result.success();
    }

    // 删除关系
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        personRelationshipService.deleteById(id);
        return Result.success();
    }

    // 查询单个关系
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Long id) {
        PersonRelationship relationship = personRelationshipService.selectById(id);
        return Result.success(relationship);
    }

    // 查询所有关系
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<PersonRelationship> list = personRelationshipService.selectAll();
        return Result.success(list);
    }

    // 根据人员ID1查询关系
    @GetMapping("/selectByPersonId1/{personId}")
    public Result selectByPersonId1(@PathVariable Long personId) {
        List<PersonRelationship> list = personRelationshipService.selectByPersonId1(personId);
        return Result.success(list);
    }

    // 根据人员ID2查询关系
    @GetMapping("/selectByPersonId2/{personId}")
    public Result selectByPersonId2(@PathVariable Long personId) {
        List<PersonRelationship> list = personRelationshipService.selectByPersonId2(personId);
        return Result.success(list);
    }

    // 分页查询
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<PersonRelationship> pageInfo = personRelationshipService.selectPage(pageNum, pageSize);
        return Result.success(pageInfo);
    }
}